//
//  NData.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "NDate.h"
#import "Logger.h"

#define MONTH_JANUARY_STR               NSLocalizedString(@"一月", @"")
#define MONTH_FEBRUARY_STR              NSLocalizedString(@"二月", @"")
#define MONTH_MARCH_STR                 NSLocalizedString(@"三月", @"")
#define MONTH_APRIL_STR                 NSLocalizedString(@"四月", @"")
#define MONTH_MAY_STR                   NSLocalizedString(@"五月", @"")
#define MONTH_JUNE_STR                  NSLocalizedString(@"六月", @"")
#define MONTH_JULY_STR                  NSLocalizedString(@"七月", @"")
#define MONTH_AUGUST_STR                NSLocalizedString(@"八月", @"")
#define MONTH_SEPTEMBER_STR             NSLocalizedString(@"九月", @"")
#define MONTH_OCTOBER_STR               NSLocalizedString(@"十月", @"")
#define MONTH_NOVEMBER_STR              NSLocalizedString(@"十一月", @"")
#define MONTH_DECEMBER_STR              NSLocalizedString(@"十二月", @"")

@implementation NDate

+ (NSString *)getWeek:(NSInteger)week
{
    NSString *str = @"";
    switch (week) {
        case 2:
            str = NSLocalizedString(@"星期一", "Monday");
            break;
        case 3:
            str = NSLocalizedString(@"星期二", "Tuesday");
            break;
        case 4:
            str = NSLocalizedString(@"星期三", @"Wednesday");
            break;
        case 5:
            str = NSLocalizedString(@"星期四", @"Thursday");
            break;
        case 6:
            str = NSLocalizedString(@"星期五", @"Friday");
            break;
        case 7:
            str = NSLocalizedString(@"星期六", @"Saturday");
            break;
        case 1:
            str = NSLocalizedString(@"星期日", @"Sunday");
            break;
        default:
            break;
    }
    return str;
}

+ (NSString *)getInOneHourTimeString:(float)duration
{
    
    
//    LogInf(@"==============getInOneHourTimeString duration:%f", duration);
    
    int m = duration/60;
    ///NSString *mmStr = (m < 10 ? [@"0" stringByAppendingFormat:@"%d", m] : [@"" stringByAppendingFormat:@"%d", m]);
    NSString *mmStr = [@"" stringByAppendingFormat:@"%d", m];
    int s = (duration - m * 60 );
    // NSString *ssStr = (s < 10 ? [@"0" stringByAppendingFormat:@"%d", s] : [@"" stringByAppendingFormat:@"%d", s]);
     NSString *ssStr = [@"" stringByAppendingFormat:@"%d", s];
   // return[[NSString alloc]initWithFormat:@"%@:%@.%@", mmStr, ssStr, NSLocalizedString(@@"前", @@"前")];
    if([mmStr isEqualToString:@"0"])
    {
        if([ssStr isEqualToString:@"1"])
            return[NSString stringWithFormat:NSLocalizedString(@"%@秒%@", @""), ssStr, NSLocalizedString(@"前", @@"前")];
        else
            return[NSString stringWithFormat:NSLocalizedString(@"%@秒%@", @""), ssStr, NSLocalizedString(@"前", @@"前")];
            
    }
    else if([mmStr isEqualToString:@"1"])
        return[NSString stringWithFormat:NSLocalizedString(@"%@分钟%@", @""), mmStr, NSLocalizedString(@"前", @@"前")];
        
    else
        return[NSString stringWithFormat:NSLocalizedString(@"%@分钟%@", @""), mmStr, NSLocalizedString(@"前", @@"前")];
}



+ (NSString *)getTimeString:(double)savedTimeInterval
{
    if(savedTimeInterval == 0)
        return @"";
    
//    LogInf(@"==============getTimeString savedTimeInterval:%f", savedTimeInterval);
    
    
    NSDate *dateInstence = [NSDate date];
    
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:dateInstence];
    
    [components setHour:-[components hour]];
    [components setMinute:-[components minute]];
    [components setSecond:-[components second]];
    NSDate *today = [cal dateByAddingComponents:components toDate:[[[NSDate alloc] init] autorelease] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight);
    
    [components setHour:-24];
    [components setMinute:0];
    [components setSecond:0];
    NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0];
    
    components = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[[[NSDate alloc] init] autorelease]];
    
    
    NSInteger week = [components weekday];
    
    [components setDay:([components day] - (week - 1))]; 
    NSDate *thisWeek  = [cal dateFromComponents:components];
    
    [components setDay:([components day] - 7)];
    //NSDate *lastWeek  = [cal dateFromComponents:components];
    
    [components setDay:([components day] - ([components day] -1))]; 
    //NSDate *thisMonth = [cal dateFromComponents:components];
    
    [components setMonth:([components month] - 1)]; 
    //NSDate *lastMonth = [cal dateFromComponents:components];
    
    
    
    //Test date;
    //savedTimeInterval = [dateInstence timeIntervalSince1970] + 45000 - 1*[dateInstence timeIntervalSinceDate:today];
    // savedTimeInterval = [dateInstence timeIntervalSince1970] + 1 - 24*3600*2;
    
    
    
    // NSTimeInterval savedTimeInterval = 1324797051 - 1 - [dateInstence timeIntervalSinceDate:today];
    NSTimeInterval nowTime = [[NSDate date] timeIntervalSince1970];
//    LogInf(@"nowTime:%f savedTime:%f", nowTime, savedTimeInterval);
    NSTimeInterval duration = nowTime - savedTimeInterval;

//    LogInf(@"==============getTimeString duration:%f", duration);
    if(duration < 0)
    {
        
        LogInf(@"========ERROR=========== nowtime:%f savedtime:%f", [[NSDate date] timeIntervalSince1970], savedTimeInterval);
    }
    
    NSTimeInterval oneHourDuration = 3600;
    NSTimeInterval todayDuration = [dateInstence timeIntervalSinceDate:today];
    NSTimeInterval yesterDayDuration = [dateInstence timeIntervalSinceDate:yesterday];
    NSTimeInterval oneWeekDuration = [dateInstence timeIntervalSinceDate:thisWeek];
    //NSTimeInterval *today2 = [datea timeIntervalSinceDate:today];
//    LogInf(@"==============[dateInstence timeIntervalSince1970]:%f", [dateInstence timeIntervalSince1970]);
//    LogInf(@"==============savedTimeInterval:%f", savedTimeInterval);
//    LogInf(@"==============duration:%f", duration);
//    LogInf(@"==============oneHourDuration:%f", oneHourDuration);
//    LogInf(@"==============todayDuration:%f", todayDuration);
//    LogInf(@"==============yesterDayDuration:%f", yesterDayDuration);
//    LogInf(@"==============oneWeekDuration:%f", oneWeekDuration);
    
    //LogInf(@"==============");

   //LogInf(@"==============duration:%f", duration);
    //LogInf(@"==============oneHourDuration:%f", oneHourDuration);
    
    if(duration < oneHourDuration)
    {
//        LogInf(@"In one hour! duration:%f", duration);
        return [self getInOneHourTimeString:duration];
    }
    else if(duration <= todayDuration)
    {
//        LogInf(@"In today");
        
        
        NSDate *thatDate = [NSDate dateWithTimeIntervalSince1970:savedTimeInterval];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        //[formatter setDateFormat:@"hh:mm:ss"];
        [formatter setDateFormat:@"HH:mm a"];
        //NSString *currentIndentifier = [[formatter locale] localeIdentifier];
        
        NSLocale *currentLocale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh"];
        
        [formatter setLocale:currentLocale];
        NSString *dateStr = [formatter stringFromDate:thatDate];
        
        //int year = [[dateArray objectAtIndex:0] intValue];
        //int month = [[dateArray objectAtIndex:1] intValue];
       // int ndate = [[dateArray objectAtIndex:2] intValue];
        [formatter release];
        [currentLocale release];
        return dateStr;
        //return [NSString stringWithFormat:@"%@ %@", 
        //        [[NSString alloc]initWithFormat:@"%d%@%d%@%d", year, @"/", month, @"/", ndate], 
        //        [formatter stringFromDate:thatDate]];
        
    }
    else if(duration <= yesterDayDuration)
    {
//        LogInf(@"In yesterday!");
        return NSLocalizedString(@"昨天", @"昨天");
    }
    else 
    {
        if(duration <= oneWeekDuration)
        {
//            LogInf(@"in this week!");
            return [self getWeek:week];
        }
        else
        {
//            LogInf(@"more then one week!");
            
            NSDate *thatDate = [NSDate dateWithTimeIntervalSince1970:savedTimeInterval];
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy MM dd hh:mm a"];
            //NSString *currentIndentifier = [[formatter locale] localeIdentifier];
            
            NSLocale *currentLocale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
            
            [formatter setLocale:currentLocale];
            NSString *dateStr = [formatter stringFromDate:thatDate];
            NSArray *dateArray = [dateStr componentsSeparatedByString:@" "];
            
            int year = [[dateArray objectAtIndex:0] intValue];
            int month = [[dateArray objectAtIndex:1] intValue];
            int ndate = [[dateArray objectAtIndex:2] intValue];
            
            
            //return [NSString stringWithFormat:@"%@ %@", 
             //       [[NSString alloc]initWithFormat:@"%d%@%d%@%d", year, @"/", month, @"/", ndate], 
             //       [formatter stringFromDate:thatDate]];
//            LogInf(@"data:%@",[NSString stringWithFormat:@"%d%@%d%@%d", year, @"/", month, @"/", ndate]);
            [formatter release];
            [currentLocale release];
            return [NSString stringWithFormat:@"%d%@%d%@%d", year, @"-", month, @"-", ndate];
        }
    }
    
    return @"Unknow time";
    
}
+ (NSString *)getSmsTimeByStr:(NSString *)timeStr
{
    NSArray *smstimeArr = [timeStr componentsSeparatedByString:@" "];
    NSArray *dateArray = [smstimeArr[0] componentsSeparatedByString:@"-"];
    int year = [[dateArray objectAtIndex:0] intValue];
    int m = [[dateArray objectAtIndex:1] intValue];
    int d = [[dateArray objectAtIndex:2] intValue];
    
    
    NSString *hhMM = [smstimeArr objectAtIndex:1];
    NSArray *hhMMArray = [hhMM componentsSeparatedByString:@":"];
    int hh = [[hhMMArray objectAtIndex:0] intValue];
    int MM = [[hhMMArray objectAtIndex:1] intValue];
    NSString *amOrPm = [smstimeArr objectAtIndex:2];
    NSString *mmStr;
    NSString *ddStr;
    if(m < 10)
    {
        mmStr = [NSString stringWithFormat:@"0%d", m];
    }
    else
    {
        
        mmStr = [NSString stringWithFormat:@"%d", m];
    }
    if(d < 10)
    {
        ddStr = [NSString stringWithFormat:@"0%d", d];
    }
    else
    {
        
        ddStr = [NSString stringWithFormat:@"%d", d];
    }
    NSString *hhStr;
    NSString *MMStr;
    if(hh < 10)
    {
        hhStr = [NSString stringWithFormat:@"0%d", hh];
    }
    else
    {
        
        hhStr = [NSString stringWithFormat:@"%d", hh];
    }
    if(MM < 10)
    {
        MMStr = [NSString stringWithFormat:@"0%d", MM];
    }
    else
    {
        
        MMStr = [NSString stringWithFormat:@"%d", MM];
    }

    
    NSString *todayDate = [self getTodayTimeString];
    NSArray *timeArr = [todayDate componentsSeparatedByString:@"-"];
    if (year < [timeArr[0] intValue]) {
        return [NSString stringWithFormat:@"%d年%@月%@日 %@ %@:%@", year, mmStr, ddStr, amOrPm, hhStr, MMStr];
    }else
    {
        if ([mmStr intValue] == [timeArr[1] intValue] && [ddStr intValue] == [timeArr[2] intValue]) {
            return [NSString stringWithFormat:@"%@ %@:%@", amOrPm, hhStr, MMStr];
        }else if ([mmStr intValue] == [timeArr[1] intValue] && ([ddStr intValue] + 1) == [timeArr[2] intValue])
        {
            return [NSString stringWithFormat:@" 昨天 %@ %@:%@", amOrPm, hhStr, MMStr];
        }else
        {
            return [NSString stringWithFormat:@"%@月%@日 %@ %@:%@", mmStr, ddStr, amOrPm, hhStr, MMStr];
        }
    }

}
+ (NSString *)getTimeByTimeInterval:(NSTimeInterval) timeInterval
{
    NSDate *thatDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy MM dd hh:mm a"];
    //NSString *currentIndentifier = [[formatter locale] localeIdentifier];
    
    NSLocale *currentLocale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    
    [formatter setLocale:currentLocale];
    NSString *dateStr = [formatter stringFromDate:thatDate];
    NSArray *dateArray = [dateStr componentsSeparatedByString:@" "];
    
    int year = [[dateArray objectAtIndex:0] intValue];
    int m = [[dateArray objectAtIndex:1] intValue];
    int d = [[dateArray objectAtIndex:2] intValue];
    
    NSString *mmStr;
    NSString *ddStr;
    if(m < 10)
    {
        mmStr = [NSString stringWithFormat:@"0%d", m];
    }
    else
    {
        
        mmStr = [NSString stringWithFormat:@"%d", m];
    }
    if(d < 10)
    {
        ddStr = [NSString stringWithFormat:@"0%d", d];
    }
    else
    {
        
        ddStr = [NSString stringWithFormat:@"%d", d];
    }
    
    
    [formatter release];
    [currentLocale release];
    return [NSString stringWithFormat:@"%d%@%@%@%@", year, @"-", mmStr, @"-", ddStr];
}

+ (NSString *)getTodayTimeString
{
    NSTimeInterval nowTime = [[NSDate date] timeIntervalSince1970];
    return [self getTimeByTimeInterval:nowTime];
}
+ (NSString *)getSundayInThisWeekTimeString
{
    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
    [components setDay:([components day]-([components weekday]-1))];
    
    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];
    [dateFormat_first setDateFormat:@"yyyy-MM-dd"];
    
    NSString *dateString_first = [dateFormat_first stringFromDate:beginningOfWeek];
    LogNor(@"First_date: %@", dateString_first);
    
    [gregorian release];
    [dateFormat_first release];
    return dateString_first;
}

+ (NSString *)getTimeByTimeInterval2:(NSTimeInterval) timeInterval
{
    NSDate *thatDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy MM dd hh:mm:ss a"];
    //NSString *currentIndentifier = [[formatter locale] localeIdentifier];
    
    NSLocale *currentLocale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    
    [formatter setLocale:currentLocale];
    NSString *dateStr = [formatter stringFromDate:thatDate];
    NSArray *dateArray = [dateStr componentsSeparatedByString:@" "];
    
    int year = [[dateArray objectAtIndex:0] intValue];
    int m = [[dateArray objectAtIndex:1] intValue];
    int d = [[dateArray objectAtIndex:2] intValue];
    
    
    NSString *hhMM = [dateArray objectAtIndex:3];
    NSArray *hhMMArray = [hhMM componentsSeparatedByString:@":"];
    int hh = [[hhMMArray objectAtIndex:0] intValue];
    int MM = [[hhMMArray objectAtIndex:1] intValue];
//    int SS = [[hhMMArray objectAtIndex:2] intValue];
    NSString *amOrPm = [[dateArray objectAtIndex:4] isEqualToString:@"PM"] ? @"下午" : @"上午";
    
    NSString *mmStr;
    NSString *ddStr;
    if(m < 10)
    {
        mmStr = [NSString stringWithFormat:@"0%d", m];
    }
    else
    {
        
        mmStr = [NSString stringWithFormat:@"%d", m];
    }
    if(d < 10)
    {
        ddStr = [NSString stringWithFormat:@"0%d", d];
    }
    else
    {
        
        ddStr = [NSString stringWithFormat:@"%d", d];
    }
    NSString *hhStr;
    NSString *MMStr;
//    NSString *ssStr;
    if(hh < 10)
    {
        hhStr = [NSString stringWithFormat:@"0%d", hh];
    }
    else
    {
        
        hhStr = [NSString stringWithFormat:@"%d", hh];
    }
    if(MM < 10)
    {
        MMStr = [NSString stringWithFormat:@"0%d", MM];
    }
    else
    {
        
        MMStr = [NSString stringWithFormat:@"%d", MM];
    }
//    if(SS < 10)
//    {
//        ssStr = [NSString stringWithFormat:@"0%d", SS];
//    }
//    else
//    {
//        
//        ssStr = [NSString stringWithFormat:@"%d", SS];
//    }
    
    [formatter release];
    [currentLocale release];
    
    NSString *todayDate = [self getTodayTimeString];
    NSArray *timeArr = [todayDate componentsSeparatedByString:@"-"];
    if (year < [timeArr[0] intValue]) {
      return [NSString stringWithFormat:@"%d年%@月%@日 %@ %@:%@", year, mmStr, ddStr, amOrPm, hhStr, MMStr];
    }else
    {
        if ([mmStr intValue] == [timeArr[1] intValue] && [ddStr intValue] == [timeArr[2] intValue]) {
           return [NSString stringWithFormat:@"%@ %@:%@", amOrPm, hhStr, MMStr];
        }else if ([mmStr intValue] == [timeArr[1] intValue] && ([ddStr intValue] + 1) == [timeArr[2] intValue])
        {
            return [NSString stringWithFormat:@" 昨天 %@ %@:%@", amOrPm, hhStr, MMStr];
        }else
        {
           return [NSString stringWithFormat:@"%@月%@日 %@ %@:%@", mmStr, ddStr, amOrPm, hhStr, MMStr];
        }
    }
    
    
}
+ (NSString *)getStartDayInThisMonthString
{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents * components = [calendar components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[[[NSDate alloc] init] autorelease]];
    [components setDay:1];
    NSInteger m = components.month;
    NSInteger y = components.year;
    NSInteger d = components.day;
    NSString *mmStr;
    NSString *ddStr;
    
    if(m < 10)
    {
        mmStr = [NSString stringWithFormat:@"0%ld", (long)m];
    }
    else
    {
        
        mmStr = [NSString stringWithFormat:@"%ld", (long)m];
    }
    if(d < 10)
    {
        ddStr = [NSString stringWithFormat:@"0%ld", (long)d];
    }
    else
    {
        
        ddStr = [NSString stringWithFormat:@"%ld", (long)d];
    }
    
    return [NSString stringWithFormat:@"%ld-%@-%@", (long)y, mmStr, ddStr];

}
@end
